package com.tms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tms.domain.Family;
import com.tms.domain.TDef;
import com.tms.domain.index.TDefUseRank;
import com.tms.mapper.develop.ToolDefMapper;
import com.tms.service.ToolDefService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 模具定义业务实现
 *
 * @author Dicomsky
 */
@Service
public class ToolDefServiceImpl implements ToolDefService {

    @Autowired
    private ToolDefMapper toolDefMapper;

    @Override
    @Cacheable(cacheNames = "tDefs", key = "#code", unless = "#result == null")
    public TDef findToolDefByCode(String code) {
        return toolDefMapper.selectByCode(code);
    }

    @Override
    @Cacheable(cacheNames = "tDefs", key = "#id", unless = "#result == null")
    public TDef findToolDefById(Integer id) {
        return toolDefMapper.selectByPrimaryKey(id);
    }

    @Override
    @Caching(
            put = {
                    @CachePut(cacheNames = "tDefs", key = "#result.id", unless = "#result == null || #result.id == 0"),
                    @CachePut(cacheNames = "tDefs", key = "#toolDef.code", unless = "#result == null || #result.id == 0")
            },
            evict = {
                    @CacheEvict(cacheNames = "tDefList", key = "#toolDef.workcell.id", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "tDefInfo", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "tDefNums", allEntries = true, beforeInvocation = true)
            }
    )
    public TDef createToolDef(TDef toolDef) {
        toolDefMapper.insertSelective(toolDef);
        return toolDef;
    }

    @Override
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = "tDefList", key = "#toolDef.workcell.id", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "tDefInfo", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "tDefs", key = "#toolDef.id"),
                    @CacheEvict(cacheNames = "tDefs", key = "#toolDef.code"),
                    @CacheEvict(cacheNames = "tDefNums", allEntries = true, beforeInvocation = true)
            }
    )
    public void deleteToolDefById(TDef toolDef) {
        toolDefMapper.deleteByPrimaryKey(toolDef.getId());
    }

    @Override
    @Caching(
            put = {
                    @CachePut(cacheNames = "tDefs", key = "#toolDef.id"),
                    @CachePut(cacheNames = "tDefs", key = "#toolDef.code")
            },
            evict = {
                    @CacheEvict(cacheNames = "tDefList", key = "#toolDef.workcell.id", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "tDefInfo", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "tDefNums", allEntries = true, beforeInvocation = true)
            }
    )
    public TDef updateToolDefById(TDef toolDef) {
        int result = toolDefMapper.updateByPrimaryKeySelective(toolDef);
        if (result == 0) {
            throw new RuntimeException("更新失败，更新结果数为0");
        }
        return toolDef;
    }

    @Override
    @Cacheable(cacheNames = "tDefList", key = "#workcellId", unless = "#result == null")
    public List<String> findToolDefByWorkcellId(int workcellId) {
        return toolDefMapper.selectByWorkcellId(workcellId);
    }

    @Override
    @Cacheable(cacheNames = "tDefInfo", key = "#page + #pageSize + #param + #content + #workcellId", unless = "#result == null")
    public PageInfo<TDef> findToolDefByContent(int page, int pageSize, String param, String content, int workcellId) {
        PageHelper.startPage(page, pageSize);
        List<TDef> list = toolDefMapper.selectByContent(param, content, workcellId);
        return new PageInfo<>(list);
    }

    @Override
    @Cacheable(cacheNames = "tDefNums", key = "'allNums'", unless = "#result == 0")
    public int findAllToolDefNums() {
        return toolDefMapper.selectToolDefNum();
    }

    @Override
    @Cacheable(cacheNames = "tDefNums", key = "'tDefAndNum'", unless = "#result == null")
    public List<TDefUseRank> findTDefAndNum() {
        return toolDefMapper.selectTDefAndUseNum();
    }

    @Override
    public Integer findFamilyIdByName(String familyName) {
        return toolDefMapper.selectFamilyId(familyName);
    }

    @Override
    public List<Family> findAllFamilies(int workcellId) {
        return toolDefMapper.selectAllFamilies(workcellId);
    }
}